package com.example.mannortycoon.controller;

import com.example.mannortycoon.entity.CompositeEntity;
import com.example.mannortycoon.service.CompositeService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.CompositeItemVO;
import com.example.mannortycoon.vo.CompositeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@Api(tags = "作物合成")
@RequestMapping("game/composite")
@RestController
public class CompositeController {

    private final CompositeService compositeService;

    @Autowired
    public CompositeController(CompositeService compositeService){
        this.compositeService = compositeService;
    }

    /**
     * app获取所有合成
     */
    @ApiOperation("app获取所有合成")
    @GetMapping("/list")
    public R listDetail(){
        Collection<CompositeVO> list = compositeService.listDetail();
        return R.ok().put("data",list);
    }


    @GetMapping("/listBySeedId/{seedId}")
    @ApiIgnore
    public R listBySeedId(@PathVariable("seedId") Long seedId){
        List<CompositeItemVO> list = compositeService.listBySeedId(seedId);
        return R.ok().put("data",list);
    }

    @PostMapping("/save/{seedId}")
    @ApiIgnore
    public R save(@PathVariable("seedId") Long seedId,@RequestBody List<CompositeEntity> list){
        compositeService.saveCompositeBySeeId(seedId,list);
        return R.ok();
    }

    @ApiOperation("app合成种子")
    @PostMapping("composite/{seedId}/{number}")
    public R composite(@PathVariable("seedId") Long seedId, @PathVariable("number")Integer number, HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return compositeService.composite(userId,number,seedId);
    }
}
